<?php
namespace app\shop\controller;
use app\ApiRest;

use app\shop\model\Cap;
use app\shop\model\Config;
use app\shop\model\Goods;
use app\shop\model\GoodsCate;
use app\shop\model\GoodsSh;
use app\shop\model\Order;
use app\shop\model\RefundOrder;
use app\shop\model\RefundOrderGoods;
use app\shop\model\User;
use app\shop\model\Wallet;
use longbingcore\wxcore\WxSetting;
use think\App;
use think\facade\Db;
use think\Request;


class IndexCap extends ApiRest
{

    protected $model;

    protected $cap_info;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Cap();

        $cap_dis[] = ['user_id','=',$this->getUserId()];

        $cap_dis[] = ['status','in',[2,3]];

        $this->cap_info = $this->model->dataInfo($cap_dis);

        if(empty($this->cap_info)){

            $this->errorMsg('你还不是楼长');
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 09:59
     * @功能说明:修改团长信息
     */
    public function capUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $this->cap_info['id']
        ];

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 15:48
     * @功能说明:个人中心
     */
    public function index(){

        $data = $this->getUserInfo();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 09:34
     * @功能说明:商品分类列表
     */
    public function goodsCateList(){

        $cate_model = new GoodsCate();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['cap_id','=',$this->cap_info['id']];

        $data = $cate_model->dataList($dis,20);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 10:03
     * @功能说明:添加商品分类
     */
    public function goodsCateAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['cap_id'] = $this->cap_info['id'];

        $cate_model = new GoodsCate();

        $res = $cate_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 10:06
     * @功能说明:编辑商品分类
     */
    public function goodsCateUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $cate_model = new GoodsCate();

        $cate_info  = $cate_model->dataInfo($dis);

        if(empty($cate_info)){

            $this->errorMsg('商品分类不存在');
        }
        //如果是删除
        if(!empty($input['status'])&&$input['status']==-1&&$cate_info['goods_num']>0){

            $this->errorMsg('该分类下存在商品，无法删除');

        }

        $res = $cate_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 10:08
     * @功能说明:商品分类下拉框
     */
    public function goodsCateSelect(){

        $cate_model = new GoodsCate();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['cap_id','=',$this->cap_info['id']];

        $data = $cate_model->where($dis)->where('top desc,id desc')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 15:06
     * @功能说明:商品各个状态下的数量
     */
    public function goodsCount(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'cap_id'  => $this->cap_info['id'],

            'status'  => 1

        ];

        $goods_model = new Goods();
        //上架数量
        $data['on_count']    = $goods_model->where($dis)->count();

        $dis['status'] = 2;
        //下架数量
        $data['off_count']   =  $goods_model->where($dis)->count();

        $dis['status'] = 3;
        //仓库数量
        $data['house_count'] =  $goods_model->where($dis)->count();

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 11:08
     * @功能说明:商品列表
     */
    public function goodsList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['cap_id','=',$this->cap_info['id']];

        $dis[] = ['status','=',$input['status']];
        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['goods_name','like','%'.$input['goods_name'].'%'];
        }

        $goods_model = new Goods();

        $data = $goods_model->dataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //创建时间
                $v['create_time'] = date('Y/m/d',strtotime($v['create_time']));

            }

        }

        return $this->success($data);

    }





    /**
     * @author chenniang
     * @DataTime: 2021-03-23 09:32
     * @功能说明:添加商品
     */
    public function goodsAdd(){

        $input = $this->_input;
        //0放入仓库 1提交审核
        $type  = !empty($input['type'])?$input['type']:0;

        unset($input['type']);

        $input['uniacid'] = $this->_uniacid;

        $input['cap_id'] = $this->cap_info['id'];

        $input['imgs']   = !empty($input['imgs'])?implode(',',$input['imgs']):'';

        $core  = new WxSetting($this->_uniacid);

        if(!empty($input['goods_name'])){

            $rest  = $core->wxContentRlue($input['goods_name']);

            if($rest['errcode'] != 0){

                return $this->error('标题含有违法违规内容');
            }

        }

        if(!empty($input['text'])){

            $rest  = $core->wxContentRlue($input['text']);

            if($rest['errcode'] != 0){

                return $this->error('标题含有违法违规内容');
            }

        }

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);
        //判断是不是需要审核
        if($config['goods_sh']==1){
            //待审核
            $input['status'] = 3;

        }else{
            //直接上架
            $input['status'] = 1;

        }

        $goods_model = new Goods();

        $id = $goods_model->dataAdd($input);
        //提交审核
        if($type==1&&$config['goods_sh']==1){

            $this->subGoodsShAction([$id]);

        }

        return $this->success(1);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 10:28
     * @功能说明:编辑商品信息
     */
    public function goodsUpdate(){

        $input = $this->_input;

        $type  = !empty($input['type'])?$input['type']:0;

        unset($input['type']);

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $input['imgs']    = !empty($input['imgs'])?implode(',',$input['imgs']):'';

        $core  = new WxSetting($this->_uniacid);

        if(!empty($input['goods_name'])){

            $rest  = $core->wxContentRlue($input['goods_name']);

            if($rest['errcode'] != 0){

                return $this->error('标题含有违法违规内容');
            }

        }

        if(!empty($input['text'])){

            $rest  = $core->wxContentRlue($input['text']);

            if($rest['errcode'] != 0){

                return $this->error('标题含有违法违规内容');
            }

        }

        $goods_model  = new Goods();

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);
        //判断是不是需要审核
        if($config['goods_sh']==1){

            $input['status'] = 3;
        }

        $res = $goods_model->goodsUpdate($dis,$input);
        //提交审核
        if($type==1&&$config['goods_sh']==1){

            $this->subGoodsShAction([$input['id']]);

        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 14:18
     * @功能说明:商品上下架
     */
    public function goodsStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $goods_model = new Goods();

        $res = $goods_model->dataUpdate($dis,['status'=>$input['status']]);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 10:29
     * @功能说明:商品详情
     */
    public function goodsInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $goods_model = new Goods();

        $res = $goods_model->dataInfo($dis);

        $cate_model = new GoodsCate();
        //分类名字
        $res['cate_name'] = $cate_model->where(['id'=>$res['cate_id']])->value('title');

        return $this->success($res);

    }


    /**
     * @param $input
     * @功能说明:提交审核方法
     * @author chenniang
     * @DataTime: 2021-03-23 14:27
     */
    public function subGoodsShAction($goods_id){

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'cap_id'  => $this->cap_info['id'],

            'order_code' => orderCode(),

            'status'     => 1,

        ];

        $goods_sh_model = new GoodsSh();

        $goods_sh_model->dataAdd($insert);

        $id = $goods_sh_model->getLastInsID();
        //提交审核
        $res = $goods_sh_model->subSh($id,$goods_id);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-23 10:30
     * @功能说明:提交商品审核
     */
    public function subGoodsSh(){

        $input = $this->_input;
        //全选
        if(!empty($input['all'])){

            $goods_model = new Goods();

            $dis = [

                'cap_id' => $this->cap_info['id'],

                'status' => 3
            ];

            $goods_id = $goods_model->where($dis)->column('id');

        }else{

            $goods_id = $input['goods_id'];
        }

        if(empty($goods_id)){

            $this->errorMsg('暂无商品');
        }
        //提交审核
        $res = $this->subGoodsShAction($goods_id);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 11:13
     * @功能说明:商品审核列表
     */
    public function shList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['cap_id','=',$this->cap_info['id']];

        if(!empty($input['status'])){

            $dis[] = ['status','=',$input['status']];
        }

        $goods_sh_model = new GoodsSh();

        $data = $goods_sh_model->dataList($dis);

        $data['sh_ing'] = $goods_sh_model->shIng($this->cap_info['id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 11:21
     * @功能说明:审核详情
     */
    public function shInfo(){

        $input = $this->_param;

        $goods_sh_model = new GoodsSh();

        $data = $goods_sh_model->dataInfo(['id'=>$input['id']]);

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-19 15:48
     * @功能说明:个人中心
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.cap_id','=',$this->cap_info['id']];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.goods_name','like','%'.$input['name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['name'].'%'];

        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }else{

            $dis[] = ['a.pay_type','>',1];

        }

        $order_model = new Order();

        $data = $order_model->indexDataList($dis,$where);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-23 11:31
     * @功能说明:商品批量上下架
     */
    public function someGoodsUpdate(){

        $input = $this->_input;

        $goods_model = new Goods();
        //全选
        if(!empty($input['all'])){

            $res = $goods_model->where('cap_id','=',$this->cap_info['id'])->where(['status'=>$input['type']])->update(['status'=>$input['status']]);

        }else{

            $res = $goods_model->where('id','in',$input['goods_id'])->update(['status'=>$input['status']]);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 17:29
     * @功能说明:退款订单详情
     *
     */
    public function refundOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.cap_id','=',$this->cap_info['id']];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.goods_name','like','%'.$input['name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['name'].'%'];

        }

        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        $refund_model = new RefundOrder();

        $data = $refund_model->indexDataList($dis,$where);

        $data['refund_ing'] = $refund_model->refundIng($this->cap_info['id']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 11:34
     * @功能说明:同意退款
     */
    public function passRefund(){

        $input = $this->_input;

        $refund_model = new RefundOrder();
        //同意退款
        $res = $refund_model->passOrder($input['id'],$input['price'],$this->payConfig(),$this->getUserId());

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-18 09:21
     * @功能说明:拒绝退款
     */
    public function noPassRefund(){

        $input = $this->_input;

        $refund_order_model = new RefundOrder();

        $res = $refund_order_model->noPassRefund($input['id']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-24 13:33
     * @功能说明:团长审核提现
     */
    public function applyWallet(){

        $input = $this->_input;

        if($input['apply_cash']>$this->cap_info['cap_cash']){

            $this->errorMsg('余额不足');
        }

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $key = 'cap_wallet'.$this->getUserId();

        $value = getCache($key);

        if(!empty($value)){

            $this->errorMsg('网络错误，请刷新重试');

        }
        //加一个锁防止重复提交
        incCache($key,1,$this->_uniacid);

        Db::startTrans();
        //减佣金
        $res = $this->model->dataUpdate(['id'=>$this->cap_info['id']],['cap_cash'=>$this->cap_info['cap_cash']-$input['apply_cash']]);

        if($res!=1){

            Db::rollback();
            //减掉
            delCache($key,$this->_uniacid);

            $this->errorMsg('申请失败');
        }

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'cap_id'  => $this->cap_info['id'],

            'apply_cash' => $input['apply_cash'],

            'order_code' => orderCode(),

            'text'       => $input['text'],

            'true_cash'  => round($input['apply_cash']*$config['cash_balance']/100,2),

            'cash_balance' => $config['cash_balance']

        ];

        $wallet_model = new Wallet();
        //提交审核
        $res = $wallet_model->dataAdd($insert);

        if($res!=1){

            Db::rollback();
            //减掉
            delCache($key,$this->_uniacid);

            $this->errorMsg('申请失败');
        }

        Db::commit();
        //减掉
        delCache($key,$this->_uniacid);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-25 17:14
     * @功能说明:楼长核销订单
     */
    public function hxOrder(){

        $input = $this->_input;

        $order_model= new Order();

        $order = $order_model->dataInfo(['id'=>$input['id']]);

        if(empty($order)){

            $this->errorMsg('订单未找到');
        }

        if($order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }

        if($order['cap_id']!=$this->cap_info['id']){

            $this->errorMsg('你不是该订单的楼长');

        }

        $refund_model = new RefundOrder();
        //判断有无申请中的退款订单
        $refund_order = $refund_model->dataInfo(['order_id'=>$order['id'],'status'=>1]);

        if(!empty($refund_order)){

            $this->errorMsg('该订单正在申请退款，请先处理再核销');

        }

        $res = $order_model->hxOrder($input['id'],$this->cap_info['id']);

        return $this->success($res);


    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-30 13:38
     * @功能说明:团长端佣金信息
     */
    public function capCashInfo(){

        $key = 'cap_wallet'.$this->getUserId();
        //减掉
        delCache($key,$this->_uniacid);

        $wallet_model = new Wallet();
        //可提现佣金
        $data['cap_cash'] = $this->cap_info['cap_cash'];
        //累计提现
        $data['cap_total_price'] = $wallet_model->capCash($this->cap_info['id']);
        //累计收益
        $data['cap_frozen_price']  = $wallet_model->capCash($this->cap_info['id'],1);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-30 14:39
     * @功能说明:团长提现记录
     */
    public function capCashList(){

        $wallet_model = new Wallet();

        $input = $this->_param;

        $dis = [

            'cap_id' => $this->cap_info['id']
        ];


        if(!empty($input['status'])){

            $dis['status'] = $input['status'];
        }
        //提现记录
        $data = $wallet_model->dataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $this->success($data);


    }









}
